using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library;
using System.Collections.Generic;
using Ext.Net;

public partial class admin_subscribergroups : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindGroups(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    private void BindGroups(string sort, int start, int limit)
    {
        ReadWriteCustomCollection<NewsletterGroup> data = null;
        int totalCount = 0;
        ++start;

        if (this._useSearch && !String.IsNullOrEmpty(txtGroupSearchName.Text))
        {
            data = NewsletterGroup.GetPagedSubscriberGroupsByGroupName(start, limit, sort, txtGroupSearchName.Text);
            totalCount = NewsletterGroup.GetSubscriberGroupCountByGroupName(txtGroupSearchName.Text);
            this._useSearch = false;
        }
        else
        {
            data = NewsletterGroup.GetPagedSubscriberGroups(start, limit, sort);
            totalCount = NewsletterGroup.GetSubscriberGroupCount();
        }
        Store1.DataSource = data;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void UpdateGroup(int id, string groupName)
    {
        NewsletterGroup grp = NewsletterGroup.Load(id);
        grp.GroupName = groupName;
        grp.Update();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void DeleteGroups(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        NewsletterGroup.DeleteSubscriberGroups(idList);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void SearchGroup()
    {
        _useSearch = true;
        Store1.DataBind();
    }
}
